<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Rent;
use App\Exports\RentExport;
use Maatwebsite\Excel\Facades\Excel;

class RentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $param  = [];
        $query  = Rent::query();
        if($request->keyword) {
            $keyword = trim($request->keyword);
            $param['keyword'] = $keyword;
            $query->where('mobile','like','%'.$keyword.'%')->orWhere('car_num','like','%'.$keyword.'%');
        }
        $list = $query->orderBy('created_at','desc')->paginate(20);
        return view('admin.rent.index',compact('list','param'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.rent.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $rs['msg']    = '操作成功';
        $rs['status'] = 'success';
        $data = $request->except('_token');
        $data['status'] =1;
        Rent::create($data);
        return redirect('zadmin/rent')->with('rs',$rs);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = Rent::find($id);
        return view('admin.rent.edit',compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $rs['msg']    = '操作成功';
        $rs['status'] = 'success';
        $data = $request->except('_token','_method');
        Rent::where('id',$id)->update($data);
        return back()->with('rs',$rs);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $rs['msg']    = '操作成功';
        $rs['status'] = 'success';
        Rent::destroy($id);
        return back()->with('rs',$rs);

    }


    public function export(Request $request)
    {
        
        $param = [];
        if($request->client_id) {
            $param['keyword'] = (int)$request->keyword;
        }
        return Excel::download(new RentExport($param), '租赁信息'.date('YmdHis').'.xlsx');   
    }
}
